package com.llkj.contractmanagement.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.llkj.contractmanagement.main.MyApplication;
import com.llkj.contractmanagement.http.Constant;
import com.llkj.contractmanagement.utils.StringUtil;


/**
 * 用户实体类
 */
public class UserInfoBean {
    // 单利模式
    private static UserInfoBean userInfoBean = null;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private boolean isFirstOpen;
    private boolean isSetAlias;     //极光推送，是否设置别名
    private String id;//用户id
    private String account;//用户手机号
    private String logo;//用户头像
    private String name;//用户名称
    private String gender;//性别
    private String token;//用户token
    private int power1;//下载权限
    private int power2;//转发权限
    private boolean isFirstLogin;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
        editor.putString(Constant.ID, id);
        editor.commit();
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
        editor.putString(Constant.ACCOUNT, account);
        editor.commit();
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
        editor.putString(Constant.LOGO, logo);
        editor.commit();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        editor.putString(Constant.NAME, name);
        editor.commit();
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
        editor.putString(Constant.GENDER, gender);
        editor.commit();
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
        editor.putString(Constant.TOKEN, token);
        editor.commit();
    }

    public int getPower1() {
        return power1;
    }

    public void setPower1(int power1) {
        this.power1 = power1;
        editor.putInt(Constant.POWER1, power1);
        editor.commit();
    }

    public int getPower2() {
        return power2;
    }

    public void setPower2(int power2) {
        this.power2 = power2;
        editor.putInt(Constant.POWER2, power2);
        editor.commit();
    }

    // 实现了线程安全，又避免了同步带来的性能影响。
    public static UserInfoBean getInstance() {
        if (userInfoBean == null) {
            synchronized (UserInfoBean.class) {
                if (userInfoBean == null) {
                    userInfoBean = new UserInfoBean(MyApplication.getInstance());
                }
            }
        }
        return userInfoBean;
    }


    private UserInfoBean(Context context) {
        super();
        init(context);
    }

    private void init(Context context) {
        sp = context.getSharedPreferences(Constant.SP_USERINFO_FILE_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    public boolean isLogin() {
        if (StringUtil.isEmpty(userInfoBean.getToken()) || StringUtil.isEmpty(userInfoBean.getId())) {
            return false;
        } else {
            return true;
        }
    }

    public boolean isFirstOpen() {
        return isFirstOpen;
    }


    public void setFirstOpen(boolean firstOpen) {
        isFirstOpen = firstOpen;
        editor.putBoolean(Constant.ISFIRSTOPEN, firstOpen);
        editor.commit();
    }

    public boolean isFirstLogin() {
        return isFirstLogin;
    }

    public void setFirstLogin(boolean firstLogin) {
        isFirstLogin = firstLogin;
        editor.putBoolean(Constant.ISFIRSTOPEN, firstLogin);
        editor.commit();
    }

    public boolean isSetAlias() {
        return isSetAlias;
    }

    public void setSetAlias(boolean setAlias) {
        isSetAlias = setAlias;
        editor.putBoolean(Constant.ISSETALIAS, setAlias);
        editor.commit();
    }

    /**
     * 删除帐号信息
     */
    public void clearUserInfo() {
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
        setFirstOpen(false);
        setFirstLogin(false);
        getAccess();
    }

    /***
     * 读取配置文件，并赋值
     */
    public void getAccess() {
        isFirstOpen = sp.getBoolean(Constant.ISFIRSTOPEN, true);
//        isSetAlias = sp.getBoolean(Constant.ISSETALIAS, false);
        id = sp.getString(Constant.ID, "");
        account = sp.getString(Constant.ACCOUNT, "");
        logo = sp.getString(Constant.LOGO, "");
        name = sp.getString(Constant.NAME, "");
        gender = sp.getString(Constant.GENDER, "");
        token = sp.getString(Constant.TOKEN, "");
        power1 = sp.getInt(Constant.POWER1, 0);
        power2 = sp.getInt(Constant.POWER2, 0);
    }

    /***
     * 写入配置文件
     */
    public void setAccess(LoginBean loginBean) {
        editor.putString(Constant.ID, loginBean.getId());
        editor.putString(Constant.ACCOUNT, loginBean.getAccount());
        editor.putString(Constant.LOGO, loginBean.getLogo());
        editor.putString(Constant.NAME, loginBean.getName());
        editor.putString(Constant.GENDER, loginBean.getGender());
        editor.putString(Constant.TOKEN, loginBean.getToken());
        editor.putInt(Constant.POWER1, loginBean.getPower1());
        editor.putInt(Constant.POWER2, loginBean.getPower2());
        editor.commit();
        getAccess();
    }

}
